//
//  CZSecureCodeController.m
//  TCProject
//
//  Created by admin on 16/2/2.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "CZSecureCodeController.h"
#import "AccountManager.h"

@interface CZSecureCodeController ()

@property (copy, nonatomic) NSString *sureCode;
@property (weak, nonatomic) IBOutlet UITextField *oldTextfield;

@property (weak, nonatomic) IBOutlet UITextField *n1Textfield;

@property (weak, nonatomic) IBOutlet UITextField *n2Textfield;

- (IBAction)okAction:(id)sender;
@property (weak, nonatomic) IBOutlet UIView *bandView;
- (IBAction)bindBankAction:(id)sender;



@end

@implementation CZSecureCodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [self getData];
}

- (void)getData
{
    BMRequest *request = [BMRequest requestWithPath:@"checkOwnerSureCodeIsNull.htm"];
    request.params = @{@"id": @([AccountManager sharedInstance].user.ID)};
    [self showHudWithhint:kDefaultMessage];
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            self.sureCode = response.rawResult[@"sureCode"];
            if ([BMUtils isEmptyString:self.sureCode]) {
                self.bandView.hidden = NO;
            } else {
                self.bandView.hidden = YES;
            }
        } else {
            [self showHint:@"获取安全码失败"];
        }
    }];
}

- (IBAction)okAction:(id)sender {
    NSString *old = _oldTextfield.text;
    NSString *new1 = _n1Textfield.text;
    NSString *new2 = _n2Textfield.text;
    
    if ([BMUtils isEmptyString:old] || [BMUtils isEmptyString:new1]) {
        [self showHint:@"请输入6-16为安全码"];
        return;
    }
    
    if (![new1 isEqualToString:new2]) {
        [self showHint:@"两次安全码输入不一致!"];
        return;
    }
    
    BMRequest *request = [BMRequest requestWithPath:@"owner/update.htm"];
    request.params = @{@"oldSureCode": old, @"sureCode": new1, @"id": @([AccountManager sharedInstance].user.ID)};
    [self showHudWithhint:kDefaultMessage];
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            [self showHint:@"安全码修改成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self showHint:@"修改安全码失败"];
        }
    }];

}
- (IBAction)bindBankAction:(id)sender {
    UIViewController *bindBankVC = [self.storyboard instantiateViewControllerWithIdentifier:@"bindBankVC"];
    [self showViewController:bindBankVC sender:nil];
}
@end
